package com.code.leetcode.idea;

import java.util.HashSet;
import java.util.Set;

/**
 * 超出时间限制。测试用例通过了，但耗时太长。
 * Time Limit Exceeded
 */
public class LeetCodeIdea005 {
    public static void main(String[] args) {
        LeetCodeIdea005 leetCodeIdea005 = new LeetCodeIdea005();
        String test = "abcfdafdsafdsafdafdafdafdsfafdsafdsafdafdsafdsafdsafdsafds1212131323214343235454534543645662436436464d";
        String result = leetCodeIdea005.longestPalindrome(test);
        System.out.println(result);
    }

    public String longestPalindrome(String s) {
        String ans = "";
        int max = 0;
        for (int i = 0; i < s.length(); i++) {
            for (int j = i+1; j <= s.length() ; j++) {
                String t = s.substring(i, j);
                if (isPalindromic(t) && t.length() > max) {
                    max = t.length();
                    ans = t;
                }
            }
        }
        return ans;
    }
    public boolean isPalindromic(String s) {
        int length = s.length();
        for (int i = 0; i < length/2; i++) {
            if (s.charAt(i) != s.charAt(length - i -1)) {
                return false;
            }
        }
        return true;
    }
}
